<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var names = ["abc", "cba", "nba", "mba"]
    // names.forEach(function (item, index, arr) {
    //   console.log(item, index, arr)
    // })

    // 版本一
    function hyForEach1(fn) {
      for (var i = 0; i < names.length; i++) {
        fn(names[i], i, names)
      }
    }

    // 版本二
    function hyForEach2(fn, arr) {
      for (var i = 0; i < arr.length; i++) {
        fn(arr[i], i, arr)
      }
    }

    hyForEach1(function (item, index, names) {
      console.log("===1", item, index, names)
    })

    hyForEach2(function (item, index, names) {
      console.log("===2", item, index, names)
    }, names)

    // 版本三
    names.hyForEach3 = function (fn) {
      for (var i = 0; i < this.length; i++) {
        fn(this[i], i, this)
      }
    }
    names.hyForEach3(function (item, index, names) {
      console.log("===3", item, index, names)
    })

    // 版本四
    // 加入到原型
    Array.prototype.hyForEach = function (fn) {
      for (var i = 0; i < this.length; i++) {
        fn(this[i], i, this)
      }
    }
    names.hyForEach(function (item, index, names) {
      console.log("===4", item, index, names)
    })



  </script>
</body>

</html>